<template>
  <div id="add">
    <el-card :body-style="{ padding: '0px' }" shadow="nerver">
      <div slot="header">
        <span>个人中心</span>
      </div>
      <!-- card body -->
      <el-form :model="form" ref="form" label-width="80px" style="margin-top:15px" v-loading="loading">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender">
            <el-option label="男" value="m"></el-option>
            <el-option label="女" value="f"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="edit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading:false,
      form: {
        username: "",
        phone: "",
        email: "",
        gender: "",
        id: ""
      }
    };
  },
  created() {
    this.getuser();
  },
  computed: {
    userInfo() {
      return this.store.get("userInfo");
    }
  },
  methods: {
    getuser() {
this.loading=true;
      this.axios
        .get(`/users/${this.userInfo.id}`)
        .then(ret => {
          this.loading=false
          let {data,error}=ret.data
          this.form.username=data.username
          this.form.phone=data.phone
          this.form.email=data.email
          this.form.gender=data.gender
        })
        .catch();
    },
    edit() {
      this.axios
        .put(`/users/edit/${this.userInfo.id}`, {
            username:this.form.username,
            phone:this.form.phone,
            email:this.form.email,
            gender:this.form.gender
          
        })
        .then(() => {
          this.$message.success('提交成功')
          this.$router.push({
            name: "users"
          });
        })
        .catch(() => {
          alert("提交失败");
        });
    }
  }
};
</script>

<style>
</style>